﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace l1cs.server.utils
{
    public class ByteArrayUtil
    {
        public static String getTerminatedString( int i, byte[] _byteArray )
        {
            StringBuilder stringbuffer = new StringBuilder();
            for ( int j = i; j < _byteArray.Length && _byteArray[j] != 0; j++ )
            {
                String str = Encoding.GetEncoding( "SJIS" ).GetString( _byteArray, 0, _byteArray.Length );
                stringbuffer.Append( str );
            }

            return stringbuffer.ToString();
        }

        public static String dumpToString( byte[] _byteArray )
        {
            StringBuilder stringbuffer = new StringBuilder();
            int j = 0;
            for ( int k = 0; k < _byteArray.Length; k++ )
            {
                if ( j % 16 == 0 )
                {
                    stringbuffer.Append( fillHex( k, 4 ) );
                    stringbuffer.Append( ": " );
                }
                stringbuffer.Append( fillHex( _byteArray[k] & 0xff, 2 ) );
                stringbuffer.Append( " " );
                if ( ++j != 16 )
                {
                    continue;
                }
                stringbuffer.Append( "   " );
                int i1 = k - 15;
                for ( int l1 = 0; l1 < 16; l1++ )
                {
                    byte byte0 = _byteArray[i1++];
                    if ( byte0 > 31 && byte0 < 128 )
                    {
                        stringbuffer.Append( (char)byte0 );
                    }
                    else
                    {
                        stringbuffer.Append( '.' );
                    }
                }

                stringbuffer.Append( "\n" );
                j = 0;
            }

            int l = _byteArray.Length % 16;
            if ( l > 0 )
            {
                for ( int j1 = 0; j1 < 17 - l; j1++ )
                {
                    stringbuffer.Append( "   " );
                }

                int k1 = _byteArray.Length - l;
                for ( int i2 = 0; i2 < l; i2++ )
                {
                    byte byte1 = _byteArray[k1++];
                    if ( byte1 > 31 && byte1 < 128 )
                    {
                        stringbuffer.Append( (char)byte1 );
                    }
                    else
                    {
                        stringbuffer.Append( '.' );
                    }
                }
                stringbuffer.Append( "\n" );
            }
            return stringbuffer.ToString();
        }

        private static String fillHex( int i, int j )
        {
            StringBuilder s = new StringBuilder();
            String hexstr = Convert.ToString( i, 16 ).ToUpper();
            for ( int k = hexstr.Length; k < j; k++ )
            {
                s.Append( "0" );
            }
            s.Append( hexstr );
            return s.ToString();
        }
    }
}
